/**
 * 市场交易时间判断工具
 */

export interface MarketHours {
  open: string;
  close: string;
  breakStart?: string;
  breakEnd?: string;
}

export interface MarketSchedule {
  timezone: string;
  hours: MarketHours;
  holidays?: Date[];
}

/**
 * 判断中国A股市场是否在交易时间
 */
export function isChinaMarketOpen(): boolean {
  const now = new Date();
  const day = now.getDay();
  const hour = now.getHours();
  const minute = now.getMinutes();
  const time = hour * 60 + minute;

  // 周末不交易
  if (day === 0 || day === 6) return false;

  // 交易时间: 9:30-11:30, 13:00-15:00
  const morningStart = 9 * 60 + 30; // 09:30
  const morningEnd = 11 * 60 + 30; // 11:30
  const afternoonStart = 13 * 60; // 13:00
  const afternoonEnd = 15 * 60; // 15:00

  return (time >= morningStart && time <= morningEnd) || 
         (time >= afternoonStart && time <= afternoonEnd);
}

/**
 * 获取市场状态
 */
export type MarketStatus = 'trading' | 'pre_market' | 'after_hours' | 'closed';

export function getChinaMarketStatus(): MarketStatus {
  const now = new Date();
  const day = now.getDay();
  const hour = now.getHours();
  const minute = now.getMinutes();
  const time = hour * 60 + minute;

  // 周末休市
  if (day === 0 || day === 6) return 'closed';

  const morningStart = 9 * 60 + 30; // 09:30
  const morningEnd = 11 * 60 + 30; // 11:30
  const afternoonStart = 13 * 60; // 13:00
  const afternoonEnd = 15 * 60; // 15:00
  const preMarketStart = 9 * 60; // 09:00

  // 交易中
  if ((time >= morningStart && time <= morningEnd) || 
      (time >= afternoonStart && time <= afternoonEnd)) {
    return 'trading';
  }

  // 盘前（9:00-9:30）
  if (time >= preMarketStart && time < morningStart) {
    return 'pre_market';
  }

  // 午休或盘后
  if (time > morningEnd && time < afternoonStart) {
    return 'closed'; // 午休
  }

  if (time > afternoonEnd) {
    return 'after_hours';
  }

  return 'closed';
}

/**
 * 获取动态刷新间隔
 * @returns 刷新间隔（毫秒），0表示停止刷新
 */
export function getRefreshInterval(): number {
  const status = getChinaMarketStatus();
  
  switch (status) {
    case 'trading':
      return 30000; // 交易中：30秒
    case 'pre_market':
      return 60000; // 盘前：60秒
    case 'after_hours':
      return 300000; // 盘后：5分钟
    case 'closed':
      return 0; // 休市：停止刷新
    default:
      return 0;
  }
}

/**
 * 获取下一个交易时段的开始时间
 */
export function getNextTradingTime(): Date | null {
  const now = new Date();
  const day = now.getDay();
  const hour = now.getHours();
  const minute = now.getMinutes();

  const result = new Date(now);
  result.setSeconds(0);
  result.setMilliseconds(0);

  // 如果是周六，跳到下周一
  if (day === 6) {
    result.setDate(result.getDate() + 2);
    result.setHours(9, 30, 0, 0);
    return result;
  }

  // 如果是周日，跳到下周一
  if (day === 0) {
    result.setDate(result.getDate() + 1);
    result.setHours(9, 30, 0, 0);
    return result;
  }

  const time = hour * 60 + minute;

  // 盘前或早晨
  if (time < 9 * 60 + 30) {
    result.setHours(9, 30, 0, 0);
    return result;
  }

  // 午休时间
  if (time >= 11 * 60 + 30 && time < 13 * 60) {
    result.setHours(13, 0, 0, 0);
    return result;
  }

  // 收盘后，跳到第二天
  if (time >= 15 * 60) {
    result.setDate(result.getDate() + (day === 5 ? 3 : 1)); // 周五跳到下周一
    result.setHours(9, 30, 0, 0);
    return result;
  }

  return null;
}

/**
 * 格式化市场状态文本
 */
export function formatMarketStatus(status: MarketStatus): string {
  const statusMap: Record<MarketStatus, string> = {
    trading: '交易中',
    pre_market: '盘前',
    after_hours: '盘后',
    closed: '休市',
  };
  return statusMap[status];
}

